﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Skynet.Framework.Common;
using AutoRegisterManager.Common;
using System.Reflection;
using System.Xml;

namespace AutoRegisterManager.Inc
{
    public partial class OfficeList : UserControl
    {
        private DataTable dtOffice = new DataTable();
        DataTable dt = new DataTable();
        //科室列表
        public DataTable DtOffice
        {
            get { return dtOffice; }
            set { dtOffice = value; }
        }
        private int startIndex = 0;
      
        public OfficeList()
        {
            InitializeComponent();
            startIndex = 0;
        }
        private void OfficeList_Load(object sender, EventArgs e)
        {
            //FrmMain.RegType = RegTypeFacade.FindAll();
            startIndex = 0;
        }
       public void DataBind()
        {
            
            lkOffice.Properties.ValueMember = "USERID";
            lkOffice.Properties.DisplayMember = "USERNAME";
            lkOffice.Properties.SortColumnIndex = 2;
            dt.Clear();
            dt = DtOffice.Clone();
            dt.Clear();
            for (int i = 0; i < 15 ; i++)
            {
                if (startIndex > DtOffice.Rows.Count - 1 )
                {
                    startIndex++;
                    continue ;
                }
                dt.ImportRow(DtOffice.Rows[startIndex]);
                startIndex++;
            }
            setValue(dt, this.office1, lblMsg1, 0);
            if (dt.Rows.Count >= 2)
            {
                SetVisable(office2, lblMsg2,true);
                setValue(dt, this.office2, lblMsg2, 1);
            }
            else
            {
                SetVisable(office2, lblMsg2,  false);
            }
            if (dt.Rows.Count >= 3)
            {
                SetVisable(office3, lblMsg3,  true);
                setValue(dt, this.office3, lblMsg3, 2);
            }
            else
            {
                SetVisable(office3, lblMsg3, false);
            }
            if (dt.Rows.Count >= 4)
            {
                SetVisable(office4, lblMsg4, true);
                setValue(dt, this.office4, lblMsg4, 3);
            }
            else
            {
                SetVisable(office4, lblMsg4, false);
            }
            if (dt.Rows.Count >= 5)
            {
                SetVisable(office5, lblMsg5, true);
                setValue(dt, this.office5, lblMsg5, 4);
            }
            else
            {
                SetVisable(office5, lblMsg5, false);
            }
            if (dt.Rows.Count >= 6)
            {
                SetVisable(office6, lblMsg6, true);
                setValue(dt, this.office6, lblMsg6, 5);
            }
            else
            {
                SetVisable(office6, lblMsg6, false);
            }
            if (dt.Rows.Count >= 7)
            {
                SetVisable(office7, lblMsg7, true);
                setValue(dt, this.office7, lblMsg7, 6);
            }
            else
            {
                SetVisable(office7, lblMsg7, false);
            }
            if (dt.Rows.Count >= 8)
            {
                SetVisable(office8, lblMsg8, true);
                setValue(dt, this.office8, lblMsg8, 7);
            }
            else
            {
                SetVisable(office8, lblMsg8, false);
            }
            if (dt.Rows.Count >= 9)
            {
                SetVisable(office9, lblMsg9, true);
                setValue(dt, this.office9, lblMsg9, 8);
            }
            else
            {
                SetVisable(office9, lblMsg9, false);
            }
            if (dt.Rows.Count >=10)
            {
                SetVisable(office10, lblMsg10, true);
                setValue(dt, this.office10, lblMsg10, 9);
            }
            else
            {
                SetVisable(office10, lblMsg10, false);
            }
            if (dt.Rows.Count >= 11)
            {
                SetVisable(office11, lblMsg11, true);
                setValue(dt, this.office11, lblMsg11, 10);
            }
            else
            {
                SetVisable(office11, lblMsg11, false);
            }
            if (dt.Rows.Count >= 12)
            {
                SetVisable(office12, lblMsg12, true);
                setValue(dt, this.office12, lblMsg12, 11);
            }
            else
            {
                SetVisable(office12, lblMsg12, false);
            }
            if (dt.Rows.Count >= 13)
            {
                SetVisable(office13, lblMsg13, true);
                setValue(dt, this.office13, lblMsg13, 12);
            }
            else
            {
                SetVisable(office13, lblMsg13, false);
            }
            if (dt.Rows.Count >= 14)
            {
                SetVisable(office14, lblMsg14, true);
                setValue(dt, this.office14, lblMsg14, 13);
            }
            else
            {
                SetVisable(office14, lblMsg14, false);
            }
            if (dt.Rows.Count >= 15)
            {
                SetVisable(office15, lblMsg15, true);
                setValue(dt, this.office15, lblMsg15, 14);
            }
            else
            {
                SetVisable(office15, lblMsg15, false);
            }
            if (startIndex % 3 > 0)
            {
                this.lblNowPage.Text = String.Format("第{0}页", (startIndex / 15)+1);
            }
            else
            {
                this.lblNowPage.Text = String.Format("第{0}页", startIndex / 15);
            }
            if (DtOffice.Rows .Count % 3 > 0)
            {
                this.lblAllPage.Text = String.Format("（共{0}页）", (DtOffice.Rows.Count / 15) + 1);
            }
            else
            {
                this.lblAllPage.Text = String.Format("（共{0}页）", DtOffice.Rows.Count / 15);
            }

        }
        //科室赋值
       private void setValue(DataTable dts, OfficeItem lb, Label lbmsg, int index)
       {
           lb.LblOffice.Text = dts.Rows[index]["OFFICE"] == null ? "无" : dts.Rows[index]["OFFICE"].ToString();
           lbmsg.Text = "可以挂号";
           lbmsg.ForeColor = Color.Green;
       }
        //设置科室可见性
       private void SetVisable(OfficeItem lb, Label lbmsg, bool isVisable)
       {
           lb.Visible = isVisable;
           lbmsg.Visible = isVisable;
       }

       private void btnNextPage_Click(object sender, EventArgs e)
       {
           if (startIndex > DtOffice.Rows.Count - 1)
           {
               MyMsg.MsgInfo("已经是最后一页了！");
               return;
           }
           DataBind();
       }
       private void btnBeforePage_Click(object sender, EventArgs e)
       {
           if (startIndex - 30 < 0)
           {
               MyMsg.MsgInfo("已经是第一页了！");
               return;
           }
           startIndex = startIndex - 30;
           DataBind();
       }
        //查询专家
       private void btnFind_Click(object sender, EventArgs e)
       {
           //Frmzhuanjia fz = new Frmzhuanjia();
          
           dt.Clear();
           if (lkOffice.Text.Length <=0 )
           {
               MyMsg.MsgInfo("请选择专家！");
               return;
           }
           DataRow row = ((DataRowView)lkOffice.Properties.GetDataSourceRowByKeyValue(lkOffice.Properties.KeyValue)).Row;
           DataSet dsdoctor = GetDoctorInfo(row["OFFICEID"].ToString());
           if (dsdoctor == null || dsdoctor.Tables[0].Rows.Count <= 0)
           {
               MyMsg.MsgInfo("该科室无医师排班信息！");
               return;
           }
           DataView dv = dsdoctor.Tables[0].DefaultView;
           dv.RowFilter = "(TECHNICALPOST LIKE '%主任医师%' OR TECHNICALPOST LIKE '%副主任医师%') AND USERID='"+row["USERID"]+"'";
           if (dv.ToTable() == null || dv.ToTable().Rows.Count <= 0)
           {
               MyMsg.MsgInfo("今天无该专家排班信息！");
               return;
           }
           //fz.doctorList1.DtDoctor = dv.ToTable();
           //fz.doctorList1.officeName = row["OFFICE"].ToString();
           //fz.doctorList1.DataBind(true);
           //fz.ShowDialog();
       }
       private void office_Click(object sender, EventArgs e)
       {
          
           //RegisterInfoData registerInfoData = new RegisterInfoData();
           //OfficeItem oi = sender as OfficeItem;
           //string s = oi.Name.Substring(6);
           //int OfficeIndex = Convert.ToInt32(oi.Name.Substring(6));
           //DataSet dsdoctor = GetDoctorInfo(dt.Rows[OfficeIndex - 1]["OFFICEID"].ToString());
           //if (dsdoctor == null || dsdoctor.Tables[0].Rows.Count <= 0)
           //{
           //    MyMsg.MsgInfo("该科室无医师排班信息！");
           //    return;
           //}
           //Frmzhuanjia fz = new Frmzhuanjia();
           ////if (SkyComm.getvalue("RegisterType") == "专家挂号")
           ////{
           //    MyAlert m = new MyAlert();
           //    m.ZjMoney = 12;
           //    m.PtMoney = 8;
           //    m.OfficeName = oi.LblOffice.Text;
           //    m.alerttype = "选择挂号";
               
           //    if (m.ShowDialog() == DialogResult.OK)
           //    {
           //        this.Paint += new PaintEventHandler(OfficeList_Paint);
           //        if (m.RegRs == 1)//普通号
           //        {

           //            try
           //            {
           //                DataView dv = dsdoctor.Tables[0].DefaultView;
           //                dv.RowFilter = "TECHNICALPOST NOT LIKE '%主任医师%' AND TECHNICALPOST NOT LIKE '%副主任医师%'";
           //                if (dv.ToTable() == null || dv.ToTable().Rows.Count <= 0)
           //                {
           //                    MyMsg.MsgInfo("该科室无医师排班信息！");
           //                    return;
           //                }
           //                fz.doctorList1.DtDoctor = dv.ToTable();
           //                fz.doctorList1.officeName = dt.Rows[OfficeIndex - 1]["OFFICE"].ToString();
           //                fz.doctorList1.DataBind(false);
           //                if (fz.ShowDialog() == DialogResult.OK)
           //                {
           //                    this.ParentForm.DialogResult = DialogResult.OK;
           //                }
           //            }
           //            catch (Exception ex)
           //            {

           //                SkynetMessage.MsgInfo(ex.Message);
           //            }
           //        }
           //        else if (m.RegRs == 2)
           //        {
           //            DataView dv = dsdoctor.Tables[0].DefaultView;
           //            dv.RowFilter = "TECHNICALPOST LIKE '%主任医师%' OR TECHNICALPOST LIKE '%副主任医师%'";
           //            if (dv.ToTable() == null || dv.ToTable().Rows.Count <= 0)
           //            {
           //                MyMsg.MsgInfo("该科室无专家排班信息！");
           //                return;
           //            }
           //            fz.doctorList1.DtDoctor = dv.ToTable();
           //            fz.doctorList1.officeName = dt.Rows[OfficeIndex - 1]["OFFICE"].ToString();
           //            fz.doctorList1.DataBind(true);
           //            if (fz.ShowDialog() == DialogResult.OK)
           //            {
           //                this.ParentForm.DialogResult = DialogResult.OK;
           //            }
           //        }
           //    }
           ////}
           ////else
           ////{
           ////    try
           ////    {
           ////        DataView dv = dsdoctor.Tables[0].DefaultView;
 
           ////        fz.doctorList1.DtDoctor = dv.ToTable();
           ////        fz.doctorList1.officeName = dt.Rows[OfficeIndex - 1]["OFFICE"].ToString();
           ////        fz.doctorList1.DataBind(false);
           ////        if (fz.ShowDialog() == DialogResult.OK)
           ////        {
           ////            this.ParentForm.DialogResult = DialogResult.OK;
           ////        }
           ////    }
           ////    catch (Exception ex)
           ////    {

           ////        SkynetMessage.MsgInfo(ex.Message);
           ////    }

           ////}
       }

       void OfficeList_Paint(object sender, PaintEventArgs e)
       {
           this.ParentForm.Show();
       }
       /// <summary>
       /// 查询排班医师信息
       /// </summary>
       /// <returns></returns>
       private DataSet GetDoctorInfo(string officeID)
       {
           DataSet ds = null;
           //ArranageRecordFacade arranageRecordFacade = new ArranageRecordFacade();

           //if ("" == officeID)
           //{
           //    return null;
           //}
           ////if (SkyComm.getvalue("RegisterType") == "专家挂号")
           ////{
           //if (this.GetServerDateTime().Date == DateTime.Now.Date)
           //{
           //    ds = arranageRecordFacade.FindDoctorInfoForAutoRegister(officeID, DateTime.Now.Date);
           //}
           //else
           //{
           //    ds = arranageRecordFacade.FindAllDoctorInfo(officeID, DateTime.Now.Date);
           //}
           //this.AddColor(ds);
           ////}
           ////else
           ////{
           ////    DataSet RegTypeSet = new DataSet();
           ////    RegisterTypeFacade RegTypeFacade = new RegisterTypeFacade();
           ////    UsersFacade usersFacade = new UsersFacade();
           ////    RegTypeSet = RegTypeFacade.FindAll();

           ////    ds = usersFacade.FindDoctorInfo(officeID, RegTypeSet.Tables[0].Select("REGISTERCLASS like '%普通%'")[0]["REGISTERTYPEID"].ToString());
           ////}

           //DataColumn dc = new DataColumn();
           //dc.ColumnName = "LASTAMOUNT";
           //dc.DataType = typeof(System.Int32);
           //ds.Tables[0].Columns.Add(dc);

           //foreach (DataRow dr in ds.Tables[0].Rows)
           //{
           //    if (dr["REGISTMAX"] == DBNull.Value)
           //    {
           //        dr["REGISTMAX"] = 0;
           //    }

           //    if (dr["REGISTREMAIN"] == DBNull.Value)
           //    {
           //        dr["REGISTREMAIN"] = 0;
           //    }

           //    dr["LASTAMOUNT"] = Convert.ToInt32(dr["REGISTMAX"]) - Convert.ToInt32(dr["REGISTREMAIN"]);
           //}

           DataSet dsSequence = ds.Clone();
           //foreach (DataRow dr in ds.Tables[0].Select("", "LASTAMOUNT desc"))
           //{
           //    dsSequence.Tables[0].ImportRow(dr);
           //}


           return dsSequence;
       }
       

       /// <summary>
       /// 判断是否还有号
       /// </summary>
       /// <param name="ds"></param>
       private void AddColor(DataSet ds)
       {
           if (ds.Tables[0].Rows.Count > 0)
           {
               ds.Tables[0].Columns.Add("COLOR", typeof(System.Int32));
               foreach (DataRow row in ds.Tables[0].Rows)
               {
                   row["COLOR"] = 0;
                   if (DBNull.Value == row["REGISTMAX"])
                   {
                       continue;
                   }

                   if (Convert.ToInt32(row["REGISTMAX"]) == Convert.ToInt32(row["REGISTREMAIN"]) && Convert.ToInt32(row["BESPOKEREMAIN"]) == Convert.ToInt32(row["BESPEAKREGISTERAMOUNT"]))
                   {
                       row["COLOR"] = 1;
                   }
               }
           }
       }
        //挂号实体
       public void InsertRegData(DataRow row,int dataindex)
       {
           //if (row == null)
           //    return;
           //try
           //{
           //    string strOfficeAddress = string.Empty;
           //    if (null != dt)
           //    {
           //        strOfficeAddress = dt.Rows[dataindex-1]["OFFICEADDRESS"].ToString().Trim();
           //    }
           //    DataRow prow=FrmMain .patientInfoData .Tables[0].Rows[0];
           //    row[RegisterInfoData.T_REGISTER_INFO_DIAGNOSEID] = prow["DIAGNOSEID"];

           //    row[RegisterInfoData.T_REGISTER_INFO_OPERATORID] = SysOperatorInfo.OperatorID;
           //    row[RegisterInfoData.T_REGISTER_INFO_OPERATEDATE] = DateTime.Now;
           //    row[RegisterInfoData.T_REGISTER_INFO_CHARGEMARK] = 0;

           //    row[RegisterInfoData.T_REGISTER_INFO_PATIENTNAME] = prow["PATIENTNAME"];
           //    row[RegisterInfoData.T_REGISTER_INFO_SEX] = prow["SEX"];

           //    row[RegisterInfoData.T_REGISTER_INFO_REGISTEROFFICEID] = dt.Rows[dataindex - 1]["OFFICEID"];
           //    row[RegisterInfoData.T_REGISTER_INFO_OFFICE] = dt.Rows[dataindex - 1]["OFFICE"];
           //    row[RegisterInfoData.T_REGISTER_INFO_REGISTERCLASS] = "普通门诊";
           //    row[RegisterInfoData.T_REGISTER_INFO_DOCTORID] = -1;
           //    row[RegisterInfoData.T_REGISTER_INFO_USERNAME] = "";

           //    if (FrmMain.RegType == null || FrmMain.RegType.Tables[0].Rows.Count <= 0)
           //    {
           //        MyMsg.MsgInfo("挂号方式加载有误！");
           //        return;
           //    }
           //    row[RegisterInfoData.T_REGISTER_INFO_CASECOST] = 0.00;
           //    row[RegisterInfoData.T_REGISTER_INFO_REGISTERFEE] = FrmMain.RegType.Tables[0].Select("REGISTERCLASS='普通门诊'")[0]["REGISTERFEE"];
           //    row[RegisterInfoData.T_REGISTER_INFO_EXAMINEMONEY] = FrmMain.RegType.Tables[0].Select("REGISTERCLASS='普通门诊'")[0]["EXAMINEMONEY"];
           //    row[RegisterInfoData.T_REGISTER_INFO_BESPEAKMONEY] = 0;//预约费EXAMINEMONEY
           //    row["CHECKFEE"] = 0;
           //    row[RegisterInfoData.T_REGISTER_INFO_BESPEAKID] = "";
           //    //
           //    row[RegisterInfoData.T_REGISTER_INFO_ADDRESS] = "";
           //    row[RegisterInfoData.T_REGISTER_INFO_TELEPHONE] = "";
           //    row[RegisterInfoData.T_REGISTER_INFO_OFFICEADDRESS] = strOfficeAddress;
           //    row[RegisterInfoData.T_REGISTER_INFO_DIAGNOSESTATE] = 0;
           //    row["BALANCEMARK"] = "1";
           //    row["EXECDATE"] = DateTime.Now;//给当前字段赋值，只是为了在中间层向“F_SCHEDUL”表的CALLTIME字段插入数据。F_SCHEDUL表的CALLTIME在数据库中不允许为空。
           //}
           //catch (Exception ex)
           //{
           //    MessageBox.Show(ex.Message);
           //}

       }
        /// <summary>
        /// 得到服务器时间
        /// </summary>
        /// <returns></returns>
       private DateTime GetServerDateTime()
       {
           //CommonFacade commonFacade = new CommonFacade();

           //DateTime dateTime = commonFacade.GetServerDateTime();
           return DateTime.Now;
       }

    }
}
